Lumen & Gesso / website · lumengesso.com / app / admin / orders / [id] / printer-order.tsx
| 1 | import { prodigiFetch } from '@/app/lib/prodigi'; |
| 2 | import { prodigiDashboardUrl } from '@/app/lib/prodigi-dashboard'; |
| 3 | import { OrderMessageTime } from '@/app/components/OrderMessageTime'; |
| 4 | |
| 5 | type PrinterStatus = { |
| 6 | stage: string; |
| 7 | details?: Record<string, string>; |
| 8 | issues?: Array<{ errorCode?: string; description?: string }>; |
| 9 | }; |
| 10 | |
| 11 | export async function PrinterOrder({ orderId, prodigiOrderId }: { orderId: string; prodigiOrderId: string | null }) { |
| 12 | const url = prodigiDashboardUrl(prodigiOrderId, process.env.PRODIGI_API_BASE_URL?.includes('sandbox')); |
| 13 | let status: PrinterStatus | null = null; |
| 14 | if (prodigiOrderId) { |
| 15 | try { |
| 16 | const response = await prodigiFetch(`Orders/${encodeURIComponent(prodigiOrderId)}`, { |
| 17 | cache: 'no-store', signal: AbortSignal.timeout(8000), |
| 18 | }); |
| 19 | if (response.ok) { |
| 20 | const result = await response.json() as { order?: { id?: string; status?: PrinterStatus } }; |
| 21 | if (result.order?.id === prodigiOrderId && typeof result.order.status?.stage === 'string') { |
| 22 | status = result.order.status; |
| 23 | } |
| 24 | } |
| 25 | } catch { |
| 26 | // The saved submission reference and dashboard stay available during API outages. |
| 27 | } |
| 28 | } |
| 29 | const checkedAt = new Date().toISOString(); |
| 30 | return ( |
| 31 | <section className="admin-editor__panel admin-printer" aria-label="Printer order"> |
| 32 | <h2 className="admin-editor__h">Printer order</h2> |
| 33 | {prodigiOrderId ? ( |
| 34 | <> |
| 35 | <p>Submission reference · <strong>{prodigiOrderId}</strong></p> |
| 36 | {status ? ( |
| 37 | <> |
| 38 | <p><strong>Confirmed by Prodigi</strong> · {status.stage}</p> |
| 39 | {status.details?.inProduction && <p>Printing · {status.details.inProduction}</p>} |
| 40 | {status.details?.shipping && <p>Shipping · {status.details.shipping}</p>} |
| 41 | {!!status.issues?.length && <div role="alert" className="auth__error"> |
| 42 | <strong>Printer needs attention</strong> |
| 43 | <ul>{status.issues.map((issue, index) => <li key={index}>{issue.description || issue.errorCode || 'Review this issue in Prodigi.'}</li>)}</ul> |
| 44 | </div>} |
| 45 | <p className="proof-workbench__muted">Checked <OrderMessageTime at={checkedAt} /></p> |
| 46 | </> |
| 47 | ) : <p role="status">Live status is unavailable. Open Prodigi to confirm the current progress of this submission.</p>} |
| 48 | </> |
| 49 | ) : <p>This order has no printer submission reference.</p>} |
| 50 | <div className="proof-workbench__buttons"> |
| 51 | <a href={url} className="btn btn--secondary btn--sm" target="_blank" rel="noopener noreferrer"> |
| 52 | {prodigiOrderId ? 'View printer order ↗' : 'Open Prodigi ↗'} |
| 53 | </a> |
| 54 | {prodigiOrderId && <a href={`/admin/orders/${orderId}`} className="btn btn--secondary btn--sm">Refresh printer status</a>} |
| 55 | </div> |
| 56 | </section> |
| 57 | ); |
| 58 | } |